<?php


namespace App\Uitls;


use Encore\Admin\Form\Field\Date;

class EncryptionKohana
{
    //把密文分割成两段，每段16个字符

    static function md5_encryption($data)
    {
        $data = md5($data);
        $left = substr($data, 0, 16);
        $right = substr($data, 16, 16);
        $data = md5($left) . md5($right);
        return md5($data);
    }


    //生成token
    static function md5_token()
    {
        $data = md5(time());
        $output = '';
        $length = 12;
        for ($a = 0; $a < $length; $a++) {
            $output .= chr(mt_rand(33, 126)); //生成php随机数
        }
        $str = md5($data . $output);

        $left = substr($str, 0, 16);
        $right = substr($str, 16, 16);
        $token = md5($left) . md5($right);
        return 'TOKEN_' . $token;

    }

    //生成盐值
    static function random_salt()
    {
        return self::random_string(6);
    }

    //生成盐值
    static function random_string($num=6)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';
        $len = strlen($chars);
        $randStr = '';
        for ($i = 0; $i < $num; $i++) {
            $randStr .= $chars[mt_rand(0, $len - 1)];
        }
        return $randStr;
    }

    //密码加盐has256
    static function salt_encryption($salt, $password)
    {
        $salt_1 = mb_substr($salt, 0, 3);
        $salt_2 = mb_substr($salt, -3);
        $pass = hash('sha256', $salt_1 . $password . $salt_2);
        return substr($pass, 0, 50);

    }

    //has256加密
    static function has256($value)
    {
        return hash('sha256', $value);
    }


}
